package com.globagames.strategicgames.command
{
	
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.globagames.isometric.core.model.SimpleObject;
	import com.globagames.strategicgames.business.*;
	import com.globagames.strategicgames.event.*;
	import com.globagames.strategicgames.model.*;
	import com.globagames.strategicgames.util.SoldierDesigner;
	
	import mx.rpc.IResponder;
	

	public class CompanyManagementCommand implements Command, IResponder
	{
		private var model:StrategicGameModelLocator = StrategicGameModelLocator.getInstance();
		var soldierDesigner:SoldierDesigner = new SoldierDesigner();
		
		private var cmEvent:CompanyManagementEvent;
		private var currentTask:String;
		public function CompanyManagementCommand()
		{		
		}
	
		public function execute( event : CairngormEvent ): void
		{	
			cmEvent = event as CompanyManagementEvent;
			currentTask = cmEvent.action;
			trace(currentTask,CompanyManagementCommand);	
			trace(model.opponentCompany.factionCode);	
			if( currentTask == "LOAD_OPPONENT_COMPANY") {
				var armyModule:IArmyModule = model.armiesModules[model.opponentCompany.factionCode] as IArmyModule;
				for(var i:int = 0 ; i < model.opponentCompany.soldiers.length ; i++ ) {
					var s:Soldier = model.opponentCompany.soldiers.getItemAt(i) as Soldier;
					trace("profile code="+s.profileCode);
					var ii:int = 0;
					for each( var element:XML in armyModule.armyDatas.soldiers.soldier ) {
						if( element.@code == s.profileCode ) {
							trace("soldier building...");
							s.profile = element;
							s.soldierIso = new SimpleObject();
							s.soldierIso.identifier = s.soldierId;
							s.soldierIso.label = s.name;
							s.soldierIso.bitmapData = soldierDesigner.getNeutralBitmap(s.isCaptain,armyModule.getBitmapForSoldierCode(s.profileCode));
						}
						ii++;
					}
					trace(s.profile.xml.@name);
					
				}
			}
			
			cmEvent.isDone = true;		
		}
		public function result( event : Object ) : void
		{	
			
		}
		
		public function fault( event : Object ) : void
		{
			trace(event.fault.rootCause.message);//, event.fault.faultCode);
		}
		
		
		
	}

}